import { getAjax, postAjax, putAjax, deleteAjax } from '@/utils/request'
import { Mutation, Action } from 'vuex'
export interface DictType {
  dictId: number | null;
  dictName: string;
  dictType: string;
  enable: boolean;
  remark: string;
}

export interface StateType {
  data: DictType;
}

export interface ModuleType {
  namespaced: boolean;
  state: StateType;
  mutations: {
    SET_DATA: Mutation<StateType>;
  };
  actions: {
    getDictType: Action<StateType, StateType>;
    getDictTypeList: Action<StateType, StateType>;
    optionSelect: Action<StateType, StateType>;
    addDictType: Action<StateType, StateType>;
    updateDictType: Action<StateType, StateType>;
    deleteDictType: Action<StateType, StateType>;
    clearCache: Action<StateType, StateType>;
  };
}

const storeModel: ModuleType = {
  namespaced: true,
  state: {
    data: {
      dictId: null,
      dictName: '',
      dictType: '',
      enable: true,
      remark: ''
    }
  },
  mutations: {
    SET_DATA: (state, data) => {
      state.data = data
    }
  },
  actions: {
    getDictType(_, id) {
      return getAjax(`/system/dict/type/${id}`, null)
    },
    getDictTypeList(_, data) {
      return getAjax('/system/dict/type/list', data)
    },
    optionSelect() {
      return getAjax('/system/dict/type/optionSelect', null)
    },
    addDictType(_, data) {
      return postAjax('/system/dict/type', data)
    },
    updateDictType(_, data) {
      return putAjax('/system/dict/type', data)
    },
    deleteDictType(_, id) {
      return deleteAjax(`/system/dict/type/${id}`, null)
    },
    clearCache() {
      return deleteAjax('/system/dict/type/clearCache', null)
    }
  }
}

export default storeModel
